import json
import os

import pandas as pd

# 示例CSV数据，实际使用时可以从文件中读取
csv_data = ""


# 将DataFrame解析为列表
def parse_csv(df):
    data = []
    for index, row in df.iterrows():
        ep_zone = int(row['ep_zone'])
        h = int(row['h'])
        m = int(row['m'])
        data.append([ep_zone, h, m])
    return data


# 创建滑动窗口
def create_sliding_windows(data, window_size, num_windows):
    windows = []
    for i in range( min(len(data) + 1 - window_size, num_windows) ):
        start_index = i
        end_index = start_index + window_size
        window = data[start_index:end_index]
        windows.append(window)
    return windows


# 将数据转换为JSON格式
def data_to_json(windows, id):
    result = {"id": id, "data": windows}
    return json.dumps(result, indent=4)


# 主函数
def main():
    file_path = "02500005_1hour_0403.csv"

    # 1 读取原始数据 并归一化处理
    csv_data = pd.read_csv(file_path)

    # 解析CSV数据
    data = parse_csv(csv_data)

    # 定义滑动窗口大小和数量
    window_size = 24
    num_windows = len(data)  # 示例中创建2个滑动窗口，你可以根据需要修改这个值

    # 创建滑动窗口
    windows = create_sliding_windows(data, window_size, num_windows)

    for i in range(len(windows)):

        # 转换为JSON格式
        json_output = data_to_json(windows[i], "101")


        out_put_path = f"data\one_hour\one_hour{window_size}\output_{i}.json.txt"
        # 确保保存路径的目录存在
        os.makedirs(os.path.dirname(out_put_path), exist_ok=True)

        # 将JSON数据写入txt文件
        with open(out_put_path, "w") as f:
            f.write(json_output)

    # 输出JSON数据
    # print(json_output)


if __name__ == "__main__":
    main()